import { useAppDispatch, useAppSelector } from '@/store';
import {
  addUserChannelAction,
  delUserChannelAction,
  getAllChannelsAction,
  getUserChannelsAction,
  setCurrentIdAction,
} from '@/store/action/channel';
import { Popup, Tabs } from 'antd-mobile';
import { differenceBy } from 'lodash';
import { useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom';
import Icon from '../Icon';
import styles from './index.module.scss';

export default function Channel() {
  const dispatch = useAppDispatch();
  const history = useHistory();
  const [visible, setVisible] = useState(false);
  const [editable, setEditable] = useState(false);
  const { userList, allList, currentId } = useAppSelector((state) => state.channel);

  useEffect(() => {
    dispatch(getUserChannelsAction());
    dispatch(getAllChannelsAction());
  }, [dispatch]);

  const restChannel = differenceBy(allList, userList, 'id');

  const handleClose = () => {
    setEditable(false);
    setVisible(false);
  };

  return (
    <div className={styles.root}>
      <Tabs
        className="tabs"
        activeKey={currentId.toString()}
        onChange={(key) => dispatch(setCurrentIdAction(Number(key)))}
      >
        {userList.map((item) => (
          <Tabs.Tab title={item.name} key={item.id} />
        ))}
      </Tabs>

      <div className="right">
        <Icon type="iconbtn_search" onClick={() => history.push('/search')} />
        <Icon type="iconbtn_channel" onClick={() => setVisible(true)} />
      </div>
      <Popup visible={visible} position="left" bodyClassName={styles.channelPopup}>
        <div className="channel-header">
          <Icon type="iconbtn_channel_close" onClick={handleClose} />
        </div>

        <div className="channel-content">
          <div className="channel-item edit">
            <div className="channel-item-header">
              <span className="channel-item-title">我的频道</span>
              <span className="channel-item-title-extra">点击删除频道</span>
              <span className="channel-item-edit" onClick={() => setEditable(!editable)}>
                {editable ? '完成' : '编辑'}
              </span>
            </div>

            <div className="channel-list">
              {userList.map((item) => (
                <span className="channel-list-item" key={item.id}>
                  {item.name}
                  {editable && (
                    <Icon
                      type="iconbtn_tag_close"
                      onClick={() => dispatch(delUserChannelAction(item.id))}
                    />
                  )}
                </span>
              ))}
            </div>
          </div>

          <div className="channel-item">
            <div className="channel-item-header">
              <span className="channel-item-title">频道推荐</span>
              <span className="channel-item-title-extra">点击添加频道</span>
            </div>
            <div className="channel-list">
              {restChannel.map((item) => (
                <span
                  onClick={() => dispatch(addUserChannelAction(item))}
                  key={item.id}
                  className="channel-list-item"
                >
                  + {item.name}
                </span>
              ))}
            </div>
          </div>
        </div>
      </Popup>
    </div>
  );
}
